﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EraNet.Core.Model.Account;
using EraNet.Services.SysManage;
using EraNet.Services.Messages;
using EraNet.Services.Account;
using EraNet.Services.Drivings;
using EraNet.Services.Test;
using EraNet.Common.Files;
using System.Web;
using EraNet.WebApp.AppCore;
using EraNet.Common;
using System.Text.RegularExpressions;

namespace EraNet.WebApp.IMS
{
    public class AccountController : IMSBasePage
    {
        #region Fields
        private readonly IDriveSchoolService _driveschoolService;
        private readonly IAreaInfoService _areainfoService;
        private readonly IMemberService _memberService;
        private readonly INoticeServices _noticeService;
        private readonly IFeedbackServices _feedbackServices;
        private readonly IExaminationService _examinationService;
        #endregion

        #region Constructors
        public AccountController(IDriveSchoolService driveschoolService, IAreaInfoService areainfoService, IMemberService memberService, INoticeServices noticeService, IFeedbackServices feedbackServices,IExaminationService examinationService)
        {
            this._driveschoolService = driveschoolService;
            this._areainfoService = areainfoService;
            this._memberService = memberService;
            this._noticeService = noticeService;
            this._feedbackServices = feedbackServices;
            this._examinationService = examinationService; 
        }
        #endregion


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Infos()
        {

            var m = _driveschoolService.GetLoginClient();

            var school = _driveschoolService.GetSchoolById(m.SchoolID);
            var exam = _examinationService.GetExamLogsBySchool(m.SchoolID, 0, 5);
            ViewBag.myTabId = "0";
            //公告
            var notic = _noticeService.SelectPager(m.SchoolID,0, 5);
            //回馈信息
            var feed = _feedbackServices.SelectPager(m.SchoolID, 0, 5);

            ViewBag.employee = m;
            ViewBag.schoolaccount = school;
            ViewBag.notice = notic;
            ViewBag.feed = feed;
            ViewBag.Exam = exam;
            return View();
        }

        #region 驾校登录
        /// <summary>
        /// 驾校登陆
        /// </summary>
        /// <returns></returns>
        public JsonResult LogOnSchool(string accountName, string Pwd, string yzm, string check)
        {
            string Rmsg = string.Empty;

            #region 为空校验
            //身份证号不能为空
            if (string.IsNullOrEmpty(accountName))
                return Json("1", JsonRequestBehavior.AllowGet); //Core.Model.Common.BaseMessage.Member_Name_IsNull;
            if (string.IsNullOrEmpty(Pwd))
                return Json("2", JsonRequestBehavior.AllowGet);//Core.Model.Common.BaseMessage.Pwd_IsNull;
            if (string.IsNullOrEmpty(yzm))
                return Json("3", JsonRequestBehavior.AllowGet);//Core.Model.Common.BaseMessage.AuthCode_IsNull;
            if (Session["GoogleCode"] != null && yzm != Session["GoogleCode"].ToString())
                return Json("4", JsonRequestBehavior.AllowGet); //Core.Model.Common.BaseMessage.AuthCode_IsNull;

            #endregion
            #region IP地址和卡号的验证
            int result = 0;//执行结果

            //验证IP对应的地市是否开放
            //if (!_areainfoService.VerifyArea()) return Json(Core.Model.Common.BaseMessage.Area_NotOpen);
            #endregion
            #region 记住密码
            HttpCookie cookie = new HttpCookie("USER_COOKIE");
            if (check == "1")
            {
                // 设置用户昵称、密码
                cookie.Values.Add("Name", accountName);

                cookie.Values.Add("Pwd", Pwd);

                // 令 Cookie 永不过期

                cookie.Expires = System.DateTime.Now.AddDays(7.0);

                // 保存用户的 Cookie
                // HttpContext.Current.Response.Cookies.Add(cookie);
                HttpContext.Response.Cookies.Add(cookie);

            }
            else  // 若记住密码未勾选则默认这次取消记住密码，则将原本存密码的Cookies生存期设为现在，则会自动销毁
            {
                if (Response.Cookies["USER_COOKIE"] != null)
                    Response.Cookies["USER_COOKIE"].Expires = DateTime.Now;
            }

            #endregion

           
            Employee emp = _driveschoolService.GetEmployee(accountName,Pwd,out result);
            if (emp != null)
            {
                //保存登录信息
                EraNet.Common.Utility.WriteCookie("DriveSchool_Member", SerializationHelper.BtySerializer(emp));
            }

            return Json(result.ToString(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 验证用户名是否存在
        /// </summary>
        /// <param name="accountName"></param>
        /// <returns></returns>
        public JsonResult ValidAccountName(string accountName, string IDCard, string Telephone)
        {
            //验证身份证格式
            if (!Common.Validator.IsIdCard(IDCard))
            {
                return Json("3", JsonRequestBehavior.AllowGet);
            }
            //验证联系电话
            if (!Common.Validator.IsMobile(Telephone) && !Regex.IsMatch(Telephone, @"(\d{3,4})-?\d{7,8}"))
            {
                return Json("4", JsonRequestBehavior.AllowGet);
            }
            Employee m = _driveschoolService.GetLoginClient();
            var editType = Common.Utility.GetCookie("editType");
            var oldAccName = Common.Utility.GetCookie("updAccName");
            var oldIdCard = Common.Utility.GetCookie("updIdCard");
            if (editType.Equals("add"))
            {
                if (_memberService.GetMemberByName(accountName, m.SchoolID) != null)
                {
                    return Json("1", JsonRequestBehavior.AllowGet);
                }
                if (_memberService.GetMemberByIdCard(IDCard, m.SchoolID) != null)
                {
                    return Json("2", JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                if (accountName != oldAccName)
                {
                    if (_memberService.GetMemberByName(accountName, m.SchoolID) != null)
                    {
                        return Json("1", JsonRequestBehavior.AllowGet);
                    }
                }
                if (IDCard != oldIdCard)
                {
                    if (_memberService.GetMemberByIdCard(IDCard, m.SchoolID) != null)
                    {
                        return Json("2", JsonRequestBehavior.AllowGet);
                    }
                }
            }

            return Json("0", JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
